package com.xmmj.mvvm_kotlin

import android.content.Context
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.xmmj.baselibrary.base.application.BaseApplication
import com.xmmj.baselibrary.network.RetrofitUtil
import com.xmmj.baselibrary.utils.Bugly.initDefault
import com.xmmj.baselibrary.utils.Density.setDensity
import com.xmmj.common.config.AppConfig
import com.xmmj.common.config.AppConfig.initConfig
import com.xmmj.common.converter.FastJsonConverterFactory
import com.xmmj.common.error.ErrorHandle
import com.xmmj.common.interceptor.ReceivedInterceptor
import com.xmmj.common.interceptor.RequestInterceptor
import com.xmmj.common.refresh.CustomFooter
import com.xmmj.common.refresh.CustomHeader

class Application : BaseApplication() {
    companion object {
        init {
            //设置全局的Header
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
                CustomHeader(context)
            }
            //设置全局的Footer
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
                CustomFooter(context)
            }
        }
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        isDebug = BuildConfig.DEBUG
        errorHandle = ErrorHandle
    }

    override fun onCreate() {
        super.onCreate()

        //APP配置（需要先配置这个）
        initConfig(this, isDebug)

        //强制设置屏幕密度以到达适配目的
        setDensity(this, 375f)

        //初始化腾讯bugly
        initDefault(this, AppConfig.BUGLY_ID, R.mipmap.ic_launcher)

        //配置Retrofit
        RetrofitUtil.baseUrl(AppConfig.API_BASE_URL)
            .connectTimeout(15 * 1000)
            .readTimeout(15 * 1000)
            .writeTimeout(15 * 1000)
            .retryOnConnection(true)
            .converter(FastJsonConverterFactory.create())
            //.certificate(File("file:///android_asset/ssl_certificate.pem"))
            .interceptors(arrayOf(ReceivedInterceptor(), RequestInterceptor()))
    }
}